<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhDealeraudit"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" :disabled="isDisabled" :click="batchAuditHandle.bind(this, 60621004)">批量同意</multiuse-button>
          <multiuse-button size="small" type="primary" :disabled="isDisabled" :click="batchAuditHandle.bind(this, 60621005)">批量驳回</multiuse-button>
          <multiuse-button size="small" type="primary" :click="exportHandle">导出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler1" />
    </Drawer>

    <BaseDialog :visible.sync="isReject" :title="'审批页面'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AuditModel :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>

    <BaseDialog :visible.sync="isDealer" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchDealer @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
import { queryList, submitByStatus, exportExcel,exporturl } from '@/api/omSale/DealerDaily/DealerQaudit';
import SonModel from './SonModel.vue';
import SearchDealer from '../../SearchDealer/SearchDealer';
import AuditModel from './AuditModel';

export default {
  name: 'DealerQaudit',
  mixins: [dictionary],
  components: {
    SearchDealer,
    SonModel,
    AuditModel
  },
  data() {
    this.BaseTable = null;
    this.conf = { show: '审核' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {
        statusList: ['60621003']
      },
      formData: {},
      handleData: [],
      exportLoading: false,
      isDealer: false,
      isDisabled: true,
      visible: false,
      isReject: false,
      handleType: '',
      drawerTitle: ''
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    dealerHandle() {
      this.isDealer = true;
    },
    emitCloseHandler(val, data) {
      this.isDealer = val;
      let dealerCodes = [];
      let dealerNames = [];
      data.map(x => {
        dealerCodes.push(x.dealerCode);
        dealerNames.push(x.dealerName);
      });
      this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = dealerCodes.toString();
    },
    emitCloseHandler1(val, type) {
      this.visible = val;
      this.isReject = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    auditHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    // 导出
    async exportHandle() {
       let json = Object.assign({}, { CODE: 'CODE_0000082' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }


      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '日报查询审批');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    selectChangeHandle(rows) {
      if (rows.length > 0) {
        this.isDisabled = false;
        this.handleData = rows;
        console.log(this.handleData, '---奥利给---');
        return;
      }
      this.isDisabled = true;
    },

    // 批量审批
    async batchAuditHandle(status) {
      let arr = [];
      this.handleData.map(x => {
        let json = {
          ownerCode: x.ownerCode,
          applyDate: x.applyDate
        };
        arr.push(json);
      });
      let params = {
        dailyStatus: status,
        ttVsDailyDTOs: arr
      };
      this.formData = params;
      this.isReject = true;
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请选择',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.dealerHandle()} />
              </div>
            );
          }
        },
        // {
        //   type: 'INPUT',
        //   label: '经销商名称',
        //   fieldName: 'dealerName',
        //   placeholder: '请输入'
        // },
        {
          type: 'RANGE_DATE', // 别忘记处理查询一天查询不到数据问题
          label: '上报日期',
          fieldName: 'applyDateStart|applyDateEnd',
          dateType: 'exactdaterange',
          placeholder: '上报日期'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.auditHandle(props.row)}>
                  审核
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '经销商简称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '区域经理',
          dataIndex: 'regionalManager',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上报状态',
          dataIndex: 'dailyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6062'),
          render: p => {
            return <span>{this.createDictText(p.row.dailyStatus, '6062')}</span>;
          }
        },
        {
          title: '上报日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'YYYY-MM-DD'
        },
        {
          title: '上报人员',
          dataIndex: 'checkPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '区域审批时间',
          dataIndex: 'regionalCheckDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大区或办事处审批时间',
          dataIndex: 'officeCheckDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
